<?php
/**
 * @link https://www.kancloud.cn/cleverstone/ymb2
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

use yii\helpers\Json;
use builder\helper\RegEx;

/* ============ 表单控件初始化JS脚本 ============ */

/** @var \yii\web\View $this View实例 */
/** @var array $controlItems 控件选项 */
/** @var string $groupField 当前分组字段 */
?>
<script>
    /** @var $scope angular当前作用域模型 */
    /** @var isInit 是否页面初始化 */
    /** @var ueEditor UE实例 */
    /** @var formOptions 表单选项 */
    /** @var xmSelect xm-select插件实例 */
    /** @var xselectOptions xm-select配置项 */
    /** @var tinyMCE_Init_Content tinyMCE富文本初始内容集 */

<?php foreach ($controlItems as $field => $item): ?>
<?php switch ($item['control']): case 'text': //文本 ?>
<?php case 'textarea': //文本域 ?>
<?php case 'number': //数字 ?>
<?php case 'password': //密码 ?>
<?php case 'range': //数字范围 ?>
<?php case 'datetime': //日期类 datetime ?>
<?php case 'date': //日期类 date ?>
<?php case 'year': //日期类 year ?>
<?php case 'month': //日期类 month ?>
<?php case 'time': //日期类 time ?>
<?php case 'hidden': //隐藏域 ?>
<?php case 'select': //下拉选择 ?>
<?php case 'radio': //单选框 ?>
<?php case 'switch': //开关 ?>
<?php case 'static_text': //静态文本 ?>
<?php case 'file': //文件上传 ?>
<?php case 'transfer': //穿梭框 ?>
<?php case 'colorpicker': //颜色拾取 ?>
<?php case 'select_layui': //下拉选择(layui select) ?>
    $scope["<?= $groupField ?>"]["<?= $field ?>"] = formOptions["<?= $field ?>"]["default"];
<?php break; case 'section': //数字区间 ?>
<?php case 'select_table': //选择表格 ?>
    $scope["<?= $groupField ?>"]["<?= $field ?>"] = formOptions["<?= $field ?>"]["default"].concat([]);
<?php break; case 'checkbox': //多选框 ?>
    $scope["<?= $groupField ?>"]["<?= $field ?>"] = ym2.deepCopy(formOptions["<?= $field ?>"]["default"]);
<?php break; case 'richtext': //富文本(UEditor) ?>
    ueEditor = ue.getInstanceById("<?= $item['__id'] ?>");
    ueEditor.ready(function () {
        ueEditor.setContent(formOptions["<?= $field ?>"]["default"]);
    });
<?php break; case 'richtext_tinymce': //富文本(TinyMCE) ?>
    if (!isInit) {
        tinyMCE.get("<?= $item['__id'] ?>").setContent(tinyMCE_Init_Content["<?= $item['__id'] ?>"], { format: "html" });
    } else {
        tinyMCE_Init_Content["<?= $item['__id'] ?>"] = document.getElementById("<?= $item['__id'] ?>").value;
    }
<?php break; case 'select_link': // 下拉联动 ?>
    $scope["<?= $groupField ?>"]["<?= $field ?>"] = formOptions["<?= $field ?>"]["default"];
    $scope["<?= $groupField . '__' . $field . '__options' ?>"] = <?= Json::encode($item['options']) ?>;
<?php break; case 'tag': // 动态标签 ?>
    if (!isInit) {
        layui.ym2FormTag.resetDom("<?= $item['__id'] ?>");
    } else {
        /**
         * @desc 配置描述
         * @see https://www.jq22.com/jquery-info23053
         * @see https://www.npmjs.com/package/sortablejs?activeTab=readme
         */
        Sortable.create(document.getElementById("<?= $item['__id'] ?>"), {
            filter: ".ym-button-new-tag",
            draggable: ".ym-tag-item",
            // animation: 150,
            animation: 300,
            // easing: "cubic-bezier(1, 0, 0, 1)",
            easing: "linear",
        });
    }
<?php break; case 'trigger': // 触发器 ?>
    <?= RegEx::js($this->renderPhpFile(__FILE__, ['controlItems' => $item['triggerItems'], 'groupField' => $groupField])) ?>
<?php break; case 'value_link': // 值连接 ?>
    <?= RegEx::js($this->renderPhpFile(__FILE__, ['controlItems' => $item['sourceControl'], 'groupField' => $groupField])) ?>
<?php break; case 'dynamic_control': // 动态控件 ?>
    <?= RegEx::js($this->renderPhpFile(__DIR__ . '/app.dynamic.control.init.php', ['controlOptions' => $item])) ?>
<?php break; case 'xselect': //xm-select普通扩展 ?>
<?php case 'xselect_tree': //xm-select下拉树扩展 ?>
<?php case 'xselect_remote': //xm-select远程搜索扩展 ?>
<?php case 'xselect_cascader': //xm-select级联模式 ?>
<?php case 'dynamic_select': //xm-select动态下拉选择 ?>
    if (isInit) {
        xselectOptions = <?= Json::encode($item['pluginOptions']) ?>;
        xselectOptions.el = "#<?= $item['__id'] ?>";
        if (xselectOptions.remoteSearch) {
            xselectOptions.remoteMethod = function (val, cb, show, pageIndex) {
                //val: 搜索框的内容, 不开启搜索默认为空, cb: 回调函数, show: 当前下拉框是否展开, pageIndex: 当前第几页
                //这里如果val为空, 则不触发搜索
                if(!val){
                    return cb([]);
                }
                var httpMethod = "<?= !empty($item['method']) ? $item['method'] : null ?>";
                var httpInstance;
                var params;
                if (httpMethod.toLowerCase() === "get") {
                    if (xselectOptions.pageRemote) {
                        params = { keyword: val, page: pageIndex || 0 };
                    } else {
                        params = { keyword: val };
                    }
                    httpInstance = $http.get(ym2.addUrlQueryParam("<?= !empty($item['url']) ? $item['url'] : null ?>", params));
                } else {
                    httpInstance = $http.post("<?= !empty($item['url']) ? $item['url'] : null ?>", params);
                }

                httpInstance.then(function (result) {
                    var data = result.data;
                    if (xselectOptions.pageRemote) {
                        cb(data.data.list || [], data.data.totalCount || 0);
                    } else {
                        cb(data.data);
                    }

                }, function (error) {
                    cb([], 0);
                    console.log(error);
                    topToastr.error(error.data || "请求失败，请在【控制台】查看错误信息！");
                });

            };
        }
        $scope["<?= $item['__id'] ?>_options"] = xselectOptions;
    } else {
        $scope["<?= $groupField ?>"]["<?= $field ?>"] = xmSelect.get("#<?= $item['__id'] ?>", true).oldValue;
    }
<?php break; case 'star': // star评分 ?>
    if (isInit) {
        $scope["<?= $item['__id'] ?>_options"] = <?= Json::encode($item['config']) ?>;
    }

    $scope["<?= $groupField ?>"]["<?= $field ?>"] = formOptions["<?= $field ?>"]["default"];
<?php break; endswitch; ?>
<?php endforeach; ?>
</script>

